package com.summer.peony.myinterface.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.summer.peony.myinterface.mapper.SServerMapper;
import com.summer.peony.myinterface.pojo.SServer;
import com.summer.peony.myinterface.pojo.SServerExample;
import com.summer.peony.myinterface.pojo.SServerExample.Criteria;
import com.summer.peony.myinterface.service.SServerService;

@Transactional
@Service
public class SServerServiceImpl implements SServerService {

	@Autowired
	private SServerMapper sServerMapper;

	@Override
	public Integer addSServer(SServer sServer) {
		// TODO 新增服务器
		sServer.setUuse(0);// 未使用
		return sServerMapper.insert(sServer);
	}

	@Override
	public Integer updateSServer(SServer sServer) {
		// TODO 修改服务器
		SServer sServer2 = sServerMapper.selectByPrimaryKey(sServer.getId());
		sServer2.setName(sServer.getName());
		sServer2.setServerAddress(sServer.getServerAddress());
		sServer2.setSpare(sServer.getSpare());
		return sServerMapper.updateByPrimaryKey(sServer2);
	}

	@Override
	public Integer deleteSServer(Integer id) {
		// TODO 删除服务器
		return sServerMapper.deleteByPrimaryKey(id);
	}

	@Override
	public PageInfo<SServer> selectAllSServer(String name, Integer uuse, Integer pageNum, Integer pageSize) {
		// TODO 查询服务列表
		SServerExample example = new SServerExample();
		Criteria criteria = example.createCriteria();
		if (uuse != null) {
			criteria.andUuseEqualTo(uuse).andNameLike("%" + name + "%");
		} else {
			criteria.andNameLike("%" + name + "%");
		}
		PageHelper.startPage(pageNum, pageSize);
		List<SServer> list = sServerMapper.selectByExample(example);
		PageInfo<SServer> pageInfo = new PageInfo<SServer>(list);
		return pageInfo;
	}

	@Override
	public Integer updateByUuse(Integer id) {
		// TODO 修改选择哪个服务器
		SServerExample example = new SServerExample();
		Criteria criteria = example.createCriteria();
		criteria.andUuseEqualTo(1);
		List<SServer> list = sServerMapper.selectByExample(example);
		if (list.size() > 0) {
			for (SServer sver : list) {
				sver.setUuse(0);
				sServerMapper.updateByPrimaryKey(sver);
			}
		}
		SServer sServer = sServerMapper.selectByPrimaryKey(id);
		sServer.setUuse(1);
		sServerMapper.updateByPrimaryKey(sServer);
		return sServerMapper.updateByPrimaryKey(sServer);
	}

}
